package com.lh.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lh.commonutils.R;
import com.lh.entity.EduCollection;
import com.lh.entity.EduCourse;
import com.lh.service.IEduCollectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lh
 * @since 2021-06-19
 */
@RestController
@RequestMapping("/eduucenter/collection")
@Api(value = "收藏中心")
public class EduCollectionController {


    @Resource
    private IEduCollectionService collectionService;


    @ApiOperation(value = "收藏课程")
    @PostMapping("addCollection")
    public R addCollection(@RequestBody EduCollection eduCollection){
        collectionService.save(eduCollection);
       return R.ok();
    }

    @ApiOperation(value = "收藏列表")
    @GetMapping("collectionList/{userid}")
    public R collectionList(@PathVariable String userid){
       List<EduCourse> list= collectionService.collectionList(userid);
        return R.ok().data("list",list);
    }
    @ApiOperation(value = "查看客户是否收藏这一门课程")
    @GetMapping("getUserCollection/{userid}/{courseid}")
    public R getUserCollection(@PathVariable String userid,@PathVariable String courseid){
        QueryWrapper<EduCollection> wrapper = new QueryWrapper<>();
        wrapper.eq("userid",userid);
        wrapper.eq("courseid",courseid);
        List<EduCollection> list = collectionService.list(wrapper);
        int i = list.size();
        return R.ok().data("size",i);
    }

    @ApiOperation(value = "取消收藏")
    @DeleteMapping("removeCollection/{userid}/{courseid}")
    public R removeCollection(@PathVariable String userid,@PathVariable String courseid){

        LambdaQueryWrapper<EduCollection> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduCollection::getCourseid,courseid);
        wrapper.eq(EduCollection::getUserid,userid);
        collectionService.remove(wrapper);
        return R.ok();

    }





}

